package com.cse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cse.constants.RedisConstants;
import com.cse.dto.Result;
import com.cse.dto.UserDTO;
import com.cse.entity.Follow;
import com.cse.mapper.FollowMapper;
import com.cse.service.IFollowService;
import com.cse.service.IUserService;
import com.cse.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.cse.constants.RedisConstants.FOLLOW_KEY;

@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;



    @Override
    public Result follow(Long followUserid, Boolean isFollow) {
        //1判断关注还是去管
        Long userId= UserHolder.getUser().getId();
        String key=FOLLOW_KEY+userId;
        if(isFollow){
            //2关注
            Follow follow=new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserid);
            boolean success = save(follow);
            if (success) {
                  stringRedisTemplate.opsForSet().add(key, followUserid.toString());
            }
        }else{
            //3取消关注 删除 delete from tb_follow where user_id =?and follow_user_id=?  利用mybatisplus提供得方法
            boolean success = remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId)
                    .eq("follow_user_id", followUserid)
            );
            if (success) {
                stringRedisTemplate.opsForSet().remove(key,followUserid.toString());
            }

        }
        return Result.ok();
    }


    @Override
    public Result isfollow(Long followUserid) {
        //获取登录用户
        Long userId = UserHolder.getUser().getId();
        //查询是否关注 select count(*) from where user_id=?and follow_user_id=?
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserid).count();
        return Result.ok(count>0);
    }

    @Override
    public Result followCommon(Long followUserId) {
        //获取用户id
        Long userId = UserHolder.getUser().getId();
        String Key1=FOLLOW_KEY+userId;
        String Key2=FOLLOW_KEY+followUserId;
        //求交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(Key1, Key2);
        if (intersect==null||intersect.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.listByIds(ids)
                .stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
